package com.sdjenkins.lab.deck;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class PersistedObject extends LoggingObject {
	
	static Session session;
	static {
		SessionFactory sf = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
		session = sf.openSession();	
	}
	
	public void save() {
		
		Transaction transaction = session.beginTransaction();;
		try {
			session.saveOrUpdate(this);
			transaction.commit();
		}
		catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}
		
	}
	
	public static void  closeSession() {
		session.close();		
	}
	
}
